package cn.sumpay.servlet;

import cn.sumpay.model.AwardsInfo;
import cn.sumpay.model.Depart;
import cn.sumpay.model.User;

import static cn.sumpay.utils.DataSourceUtil.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * Created by Administrator on 2017/1/3.
 */
public class Index extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        //判断是否有用户
        if(session.getAttribute("user") == null){
            System.out.println("没有登录或session失效");
            response.sendRedirect(request.getContextPath());
        }
        /*//如果ServeltContext中没有数据,从数据库获取数据
        if(session.getServletContext().getAttribute("datalist")==null)*/
        try {
            Connection connection = getConnection();


            //根据活动id和登录用户id来获取 所有的奖项候选人信息
            String sql = "select b.active_name,a.* from VOTE_AWARDS_INFO a inner join vote_active b on a.active_id=b.active_id " +
                    "where a.active_id = ? and a.awards_id in " +
                    "(select awards_id from vote_limit_user where user_id = ?)";
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setInt(1,Integer.valueOf((String) request.getSession().getAttribute("active_id")));
            pst.setInt(2,((User)session.getAttribute("user")).getUser_id());
            ResultSet rs = pst.executeQuery();
            //数据
            List<AwardsInfo> datalist = new ArrayList<AwardsInfo>();
            while(rs.next()){
                String active_name = rs.getString(1);
                Integer active_id = rs.getInt(2);

                Integer adwards_Id = rs.getInt(3);
                String adwards_name = rs.getString(4);
                Integer adwards_number = rs.getInt(5);
                Integer adwards_type = rs.getInt(6);
                String[] cand_users_id = {};
                String[] cand_users_name = {};
                String[] cand_departs_id = {};
                String[] cand_departs_name ={};
                if(rs.getString(7)!=null)
                    cand_users_id = rs.getString(7).split(",");
                if(rs.getString(8)!=null)
                    cand_users_name = rs.getString(8).split(",");
                if(rs.getString(9)!=null)
                    cand_departs_id = rs.getString(9).split(",");
                if(rs.getString(10)!=null)
                    cand_departs_name = rs.getString(10).split(",");

                List<User> cand_users = new ArrayList<User>();
                List<Depart> cand_departs = new ArrayList<Depart>();
                for(int i=0;i<cand_users_id.length;i++){
                    User user = new User();
                    user.setUser_id(Integer.valueOf(cand_users_id[i]));
                    user.setUser_name(cand_users_name[i]);
                    cand_users.add(user);
                }
                Integer userdepartid = ((User)session.getAttribute("user")).getDepart_id();
                for(int i=0;i<cand_departs_id.length;i++){
                    if(!userdepartid.equals(Integer.valueOf(cand_departs_id[i]))) {
                        Depart depart = new Depart(Integer.valueOf(cand_departs_id[i]), cand_departs_name[i]);
                        cand_departs.add(depart);
                    }
                }

                AwardsInfo awardsInfo = new AwardsInfo(active_id,active_name,adwards_Id,adwards_name,adwards_number,adwards_type,cand_users,cand_departs);
                datalist.add(awardsInfo);
            }
            request.getServletContext().setAttribute("datalist",datalist);

        } catch (SQLException e) {
            e.printStackTrace();
        }


        request.getRequestDispatcher("/WEB-INF/jsps/index.jsp").forward(request,response);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
